<template>
  <PanerForm :isShowBarDrawer="true" :caption="'编辑单元'">
    <template #body>
      <EditForm ref="editForm" />
    </template>
    <template #bar-drawer>
      <button @click="handleSubmit" class="d-btn d-btn-primary" type="button">提交</button>
      <button v-if="!isEdit" @click="handleReset" class="d-btn d-btn-default" type="reset">重置</button>
      <a v-d-goback class="d-btn d-btn-default">返回</a>
    </template>
  </PanerForm>
</template>

<script>
import { PanerForm } from '@daelui/vdog/dist/pages'
import { Dialoger, FormCalendar } from '@daelui/vdog/dist/components'
import EditForm from './edit-form'
import { zh as locale } from '@daelui/dogjs/dist/locale'
import unitSer from '../../../service/action/exeunit.action.js'

export default {
  components: {
    PanerForm,
    FormCalendar,
    Dialoger,
    EditForm
  },

  data () {
    return {
      isEdit: false // 是否是编辑模式
    }
  },

  methods: {
    /**
     * @function 提交
    */
    handleSubmit () {
      let form = this.$refs.editForm
      let state = form.state
      let defer = null
      delete state.createTime
      if (!form.valid()) {
        return false
      }
      // 编辑
      if (this.isEdit) {
        defer = unitSer.editItem(state)
      } else { // 添加
        defer = unitSer.addItem(state)
      }
      defer.then(result => {
        if (result.success) {
          Dialoger.success(locale.action.success)
        } else {
          Dialoger.danger(locale.action.fail)
        }
        // 添加则回到列表页
        if (!this.isEdit) {
          this.$router.push(this.getMicroRoute('/process/exeunit/list'))
        }
      })
    },

    /**
     * @function 重置
    */
    handleReset () {

    }
  },

  mounted () {
    let params = this.$route.params
    // 新建
    if (!params.id) {
      this.isEdit = false
    } else { // 编辑
      this.isEdit = true
      this.$refs.editForm.loadItem(params.id)
    }
  }
}
</script>

<style scoped>
.d-form-label {
  width: 90px;
}
.d-form-textarea {
  min-height: 120px;
}
</style>
